<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/5/28
 * Time: 16:18
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class Announcement extends Base {
    /**
     * 首页
     */
    public function index(){
        if(request()->isAjax()){
            $map = array();
            extract(input());
            if(isset($xq_id)&&$xq_id!="")
            {
                $map['a.wy_xq_id'] = $xq_id;
            }
            //注册时间
            if(isset($start)&&$start!=""&&isset($end)&&$end=="")
            {
                $map['a.wy_time'] = ['>= time',$start];
            }
            if(isset($end)&&$end!=""&&isset($start)&&$start=="")
            {
                $map['a.wy_time'] = ['<= time',$end];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end!="")
            {
                $map['a.wy_time'] = ['between time',[$start,$end]];
            }

            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('wy_notice')->alias('a')
                ->join('up_xq ux','ux.xq_id = a.wy_xq_id','left')
                ->join('up_admin ua','ua.id = a.wy_admin_id','left')
                ->where($map)->count();// 获取总条数
            $lists = Db::name('wy_notice')->alias('a')
                ->join('up_xq ux','ux.xq_id = a.wy_xq_id','left')
                ->join('up_admin ua','ua.id = a.wy_admin_id','left')
                ->where($map)
                ->page($NowPage,$limits)
                ->order('wy_id desc')
                ->field('a.*,ux.xq_name,ua.username')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $info = Db::table('up_xq')->select();
        $this->assign('info',$info);
        return $this->fetch();
    }
    public function add(){
        if(Request::instance()->isPost()){
            $title = input('param.banner_title');
            $content = input('param.content');
            $xq_id = explode(',',input('param.xq_id'));
            foreach ($xq_id as $key=>$vo){
                $data['wy_xq_id'] = $vo;
                $data['wy_title'] = $title;
                $data['wy_content'] = $content;
                $data['wy_time'] = date('Y-m-d H:i:s',time());
                $data['wy_admin_id'] = session('uid');
                $res = Db::table('up_wy_notice')->insertGetId($data);


                //查询这个小区下面的用户
                $where['xq_id'] = $vo;
                $where['xq_sh_status'] = 2;
                $result = Db::table('up_xq_property')->distinct(true)->where($where)->column('xq_user_id');
                $list = [];
                foreach($result as $k => $val){
                    $v = (string)$val;
                    array_push($list, $v);
                }
                $code=2;
                $str = array('msg'=>$title,'type'=>2,'wy_id'=>$res);
                $str1 = $title;
                try{
                    $this->sendJpush(1, $list, $str1, $str,$code);
                }catch(\Exception $e){

                }

            }

//            $res = Db::table('up_wy_notice')->insertAll($dataAll);
                return json(['code' => 200, 'msg' => '添加成功']);

        }
        $info = Db::table('up_xq')->select();
        $this->assign('info',$info);
        return $this->fetch();
    }
}